home *** CD-ROM | disk | FTP | other *** search
- Path: news.ccs.queensu.ca!news
- From: Wintermute <3mal5@qlink.queensu.ca>
- Newsgroups: comp.lang.c++
- Subject: Re: Callbacks using member functions
- Date: 17 Jan 1996 18:35:14 GMT
- Organization: System Infinity
- Message-ID: <4djfh2$1oo@knot.queensu.ca>
- References: <4dheva$rjl@news1.usa.pipeline.com>
- NNTP-Posting-Host: free2-slip218.tele.queensu.ca
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (Macintosh; I; 68K)
- X-URL: news:4dheva$rjl@news1.usa.pipeline.com
-
- I received the following email reply from one Rick Whitehouse (who could
- not get his Netscape to post it). It seems to be what I am looking for,
- and it seems like it will work (will try it in next few days).
-
- [begin insertion]
-
- I had the same problem you are. We are writing Motif applications in
- C++ and use member functions as call-backs. You are correct about the
- 'this'
- pointer messing things up. The call-back expects the widget pointer to
- be
- first, but the compiler sends the 'this' pointer first. The solution is
- to
- declare the member functions as static. If you remember from the LRM,
- when you
- declare a member function as static, it loses it's intrinsic membership
- attributes: i.e. you have to explicitly send the object pointer and used
- the
- qualified notation when you call it. This is perfect for callback
- purposes!!
-
- Here is what you need to do to complete the entire implementation:
-
- 1) Declare the member functions as static in the class declaration.
- 2) Declare the functions in the normal C manner.
- 3) Provide the implementation the same as any other member function.
-
- An example will clear this up.
-
- In the class' public header file, MyClass.hh:
-
- class MyClass
- {
- public :
-
- MyClass ();
-
- ~MyClass ();
-
- private :
-
- static void MyCallback (Widget w,
- XtPointer clientData,
- XtPointer callData);
- };
-
- Since all of the code will be defined in the context of the class,
- these
- member functions can be public, protected or private. I choose private
- because
- of the way I structure my classes and applications.
-
- A *private* header file comes in handy. This is like the private
- header
- files used with the widgets themselves. In this private header file,
- MyClassP.hh:
-
- #include "MyClass.hh"
-
- void MyCallback (Widget w,
- XtPointer clientData,
- XtPointer callData);
-
- Then, in the implementation file, the implementation of the callback is
- like any other member function (MyClass.cc):
-
- #include "MyClassP.hh"
-
- MyClass::MyClass ()
- {
- }
-
- MyClass::~MyClass()
- {
- }
-
- MyClass::MyCallback (Widget w,
- XtPointer clientData,
- XtPointer callData)
- {
- }
-
- Now, remember I said that a static member function loses some of its
- membership attributes? Here is where not having the 'this' pointer is a
- bless-
- ing and a curse. Usually, you would want to do something useful in the
- object,
- otherwise you wouldn't be putting the call-back in the class in the
- first place.
- When you add the call-back to the appropriate widget's callback list,
- you need
- to supply the 'this' pointer. This is most easily done in the class'
- constructor and use the 'this' pointer as your clientData parameter:
-
- XtAddCallback (w, XmNdestroyCallback, MyCallback, (XtPointer)this);
-
- Then, in your call-back, you get to you object like this:
-
- MyClass::MyCallback (Widget w,
- XtPointer clientData,
- XtPointer callData)
- {
- MyClass *myClass= (MyClass *)clientData;
- .
- .
- .
- }
-
- You can get around the automatic variable and the type-cast by
- declaring
- the second parameter to your call-back as a pointer to the object from
- the start
- and then use it directly in your call-back.
-
- Please let me know if this helps or not, and just the fact that you got
- this message (since I am having trouble with Netscape). Good luck!!
-
- Rick Whitehouse
-
- [end insertion]
-
- Mostly, I realize C++ doesn't fit flawlessly with MOTIF, but like having
- everything wrapped up in its own class; just my preferred design style.
-
- --
- Wintermute <3mal5@qlink.queensu.ca> <http://qlink.queensu.ca/~3mal5/>
-
- "If I really knew how to write, I could write something that someone
- could read and it would kill them." - william s. burroughs
-
-
-